from PyQt5.QtGui import  QImage, QPixmap
from multiprocessing import Process
import numpy as np  
import cv2 
import threading
import datetime
import time
from PyQt5.QtCore import *
#from Lib.ft2 import *
"""相机操作类""" 
class CvCamera(QObject):
    playVideoSignal = pyqtSignal(QPixmap)
    def __init__(self,qlabel,width,height):
       #获取当前相机
       self.cap = cv2.VideoCapture('http://127.0.0.1:8080/?action=stream')
       ret, frame = self.cap.read()
       #设置相机图像格式
       self.cap.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc('M','J','P','G'))
       #设置显示图像宽高
       #self.cap.set(cv2.CAP_PROP_FRAME_WIDTH,1024)
       #self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT,600)

       self.experimentId = ""
       self.experimentStepIndex = 0
       self.experimentFlag = False

       #self.ft = put_chinese_text('/home/pi/Downloads/11/Lib/msyh.ttf')
      
       #设置相机FPS
       self.cap.set(cv2.CAP_PROP_FPS ,30)
       self.fourcc = None
       self.out = None
       self.workFlag = False
       self.width = width
       self.height = height
       self.viewLabel = qlabel
       self.viewLabel.setVisible(False)
       super().__init__()

    def start(self):
        print("开启1")
        try:
            if(self.workFlag==False):
                self.cap = cv2.VideoCapture('http://127.0.0.1:8080/?action=stream')
                ret, frame = self.cap.read()
                #设置相机图像格式
                self.cap.set(cv2.CAP_PROP_FOURCC,cv2.VideoWriter_fourcc('M','J','P','G'))
                self.cap.set(cv2.CAP_PROP_FPS ,30)
                self.show()
                self.workFlag = True
                #单独线程供相机使用
                t = threading.Thread(target=self.videoWork,args=(self.viewLabel,self.width,self.height))
                t.start()
        except Exception as e:
            print(e)
    def stop(self):
        self.hide()
        self.workFlag = False

    #显示图像
    def show(self):
       self.viewLabel.setVisible(True)
    #隐藏图像
    def hide(self):
       self.viewLabel.setVisible(False)
    #释放资源
    def release(self):
       self.workFlag = False
       time.sleep(0.2)
       if(self.out):
           self.out.release()
       self.cap.release()
       


    #相机图像分析
    def videoWork(self,qlable,vwidth,vheight):
        #qlable.resize(vwidth,vheight)
        avgFrameCount = 0
        d = None
        startTime = 0
        experimentFlag=False
        self.startExperimentTime=0
        while(self.workFlag):
            try:
                # 统计开始时间
                # 从摄像头逐帧捕获数据
                ret, frame = self.cap.read()
                height, width, bytesPerComponent = frame.shape
                bytesPerLine = bytesPerComponent * width

                cv2.putText(frame, 'tanyo',(6, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX,
                            0.80, (0, 0, 255), 1)
                frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
                self.image = QImage(frame.data,width,height,bytesPerLine, QImage.Format_RGB888)
                #qlable.setPixmap(QPixmap.fromImage(self.image).scaled(vwidth,vheight))
                self.playVideoSignal.emit(QPixmap.fromImage(self.image).scaled(vwidth,vheight))
                

            except Exception as e:  
                print(e)
    
